package com.orangelife.mobile.myhouse.activity;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.google.gson.Gson;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.activity.BaseCompleteActivity;
import com.orangelife.mobile.myhouse.entity.IndustryEntity;
import com.orangelife.mobile.myhouse.fragment.IndustryFragment2;
import com.orangelife.mobile.myhouse.fragment.TenantCertOkFragment;
import com.orangelife.mobile.util.JsonLocalFileUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 认证结果界面
 * Created by 吴格非 on 2015/12/3.Modify by wgf on 20160629.
 *
 * @author wgf
 */
public class CertOkActivity extends BaseCompleteActivity implements BaseCompleteActivity.CallBackEvent {
    /** 底部列表界面 */
    @Bind(R.id.flContainer)
    FrameLayout flContainer;
    /**Activit 名称**/
    final  String MODULE_NAME="/IA/IASuccess";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CurryApplication.getInstance().addActivity(this);
        setCallBackEvent(this);
        initTitle();
        getIntentInfo();
        setTvMessageTitle("认证成功");
        DataTrack.browseTrack(DataTrack.APP_NAME,MODULE_NAME,null);
    }

        private void getIntentInfo() {
        if (getIntent().getExtras() != null) {
            IndustryEntity ib = (IndustryEntity) IntentHelper.getInstances().getSerializable(this);
            if (ib != null) {
                showOwnerHouseList(ib);
            }
        } else {
            showTenantCertOk();
        }
    }

    /**
     * 数据采集体不要删除
     *
     * @return
     */
    public Map<String, Object> getDataTrack() {
        Map<String, Object> map = new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        return map;
    }


    private void showTenantCertOk() {
        flContainer.setVisibility(View.VISIBLE);
        TenantCertOkFragment fragment = new TenantCertOkFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.flContainer, fragment).commit();
    }

    private void showOwnerHouseList(IndustryEntity ib) {
        //有停车位或者有房子都可以显示列表
        if (ib != null && ib.getApartments() != null && ib.getApartments().size() > 0) {
            flContainer.setVisibility(View.VISIBLE);
            IndustryFragment2 frgmIndustry = new IndustryFragment2();
            getSupportFragmentManager().beginTransaction().add(R.id.flContainer, frgmIndustry).commit();
            frgmIndustry.setData(ib);
            setTvMessageInfo(getString(R.string.tip_have_other_thing));
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setTextColor(getResources().getColor(R.color.title_orange));
            tvRight.setText(getString(R.string.skip));
        } else {
            flContainer.setVisibility(View.GONE);
            tvRight.setVisibility(View.GONE);
            setTvMessageInfo("");
            setTimeInfo("后返回");
        }
    }

    /** 如果返回有数据，则展示房屋列表 */
//    private void showHouseList(IndustryBean ib) {
//        setTvMessageTitle("认证成功");
//
//        //有停车位或者有房子都可以显示列表
//        if (ib != null && (IndustryBean.HAS_PARKING_SPACE.equals(ib.getHasParkingSpace()) || (ib.getHouseList() != null && ib.getHouseList().size() > 0))) {
//            flContainer.setVisibility(View.VISIBLE);
//            frgmIndustry = new IndustryFragment();
//            getSupportFragmentManager().beginTransaction().add(R.id.flContainer, frgmIndustry).commit();
//            frgmIndustry.setData(ib);
//            setTvMessageInfo(getString(R.string.tip_have_other_thing));
//            tvRight.setVisibility(View.VISIBLE);
//            tvRight.setTextColor(getResources().getColor(R.color.title_orange));
//            tvRight.setText(getString(R.string.skip));
//        } else {
//            flContainer.setVisibility(View.GONE);
//            tvRight.setVisibility(View.GONE);
//            setTvMessageInfo("");
//            setTimeInfo("后返回");
//        }
//    }

    /*##################标题栏start###################*/
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvRight)
    TextView tvRight;

    @OnClick({R.id.ib_title_left, R.id.tvRight})
    public void onClickTitle(View view) {
        switch (view.getId()) {
            case R.id.ib_title_left:
                DataTrack.clickTrack(DataTrack.APP_NAME,MODULE_NAME,"complete");
                CurryApplication.getInstance().finishActivity(this);
                break;
            case R.id.tvRight:
                CurryApplication.getInstance().finishActivity(this);
                IntentHelper.getInstances().getIntent(CertOkActivity.this, HouseListActivity.class);
                if(tvRight.getText().toString().equals("跳过")){
                    DataTrack.clickTrack(DataTrack.APP_NAME,MODULE_NAME,"skip");
                }
                break;
        }
    }

    private void initTitle() {
        tvTitle.setText("身份认证");
    }

    public void onBackClick(View view) {
        DataTrack.clickTrack(DataTrack.APP_NAME,MODULE_NAME,"skip");
        CurryApplication.getInstance().finishActivity(this);
    }

    @Override
    public void setBtnSubmit() {

    }

    @Override
    public void JumpToActivity() {

        IntentHelper.getInstances().getIntent(CertOkActivity.this, HouseListActivity.class);
    }
    /*##################标题栏end###################*/
    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
}
